package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.domain.EmployeeRole;
import com.li.mapper.DeptMapper;
import com.li.mapper.EmployeeRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.mapper.EmployeeMapper;
import com.li.domain.Employee;
import com.li.service.EmployeeService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService{

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    EmployeeRoleMapper employeeRoleMapper;

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<Employee> listPage(Employee employee, Integer page, Integer limit) {
        Integer indexpage=(page-1)*limit;
        if (employee==null){
            employee=new Employee();
        }
        Integer deptId = employee.getDeptId();
        if (deptId!=null && !"".equals(deptId)){
            employee.setDeptIdsInt(deptMapper.getdeptIdsById(deptId));
        }
        return employeeMapper.listPage(employee,indexpage,limit);
    }

    @Override
    public Integer listPageCount(Employee employee) {
        if (employee==null){
            employee=new Employee();
        }
        return employeeMapper.listPageCount(employee);
    }

    /**
     * 添加，包含角色
     * @param employee
     */
    @Transactional
    @Override
    public void addEmp(Employee employee) {
        employee.setCreateTime(new Date());
        //设置id
        String eid=UUID.randomUUID().toString();
        employee.setId(eid);
        //存入员工
        this.save(employee);
        Integer[] roleIds = employee.getRoleIds();
        if (roleIds!=null){
            for (Integer rid:roleIds){
                EmployeeRole employeeRole = new EmployeeRole();
                employeeRole.setEmployeeId(eid);
                employeeRole.setRoleId(rid);
                //添加员工角色表
                employeeRoleMapper.insert(employeeRole);
            }
        }
    }

    @Transactional
    @Override
    public void updateEmp(Employee employee) {
        String eid=employee.getId();
        //先删除
        employeeRoleMapper.delete(new QueryWrapper<EmployeeRole>().eq("employee_id",eid));
        //在添加
        //修改员工
        employeeMapper.updateById(employee);
        Integer[] roleIds = employee.getRoleIds();
        if (roleIds!=null){
            for (Integer rid:roleIds){
                EmployeeRole employeeRole = new EmployeeRole();
                employeeRole.setEmployeeId(eid);
                employeeRole.setRoleId(rid);
                //添加员工角色表
                employeeRoleMapper.insert(employeeRole);
            }
        }
    }

    @Transactional
    @Override
    public void delEmp(Employee employee) {
        String eid=employee.getId();
        //先删除中间表
        employeeRoleMapper.delete(new QueryWrapper<EmployeeRole>().eq("employee_id",eid));
        employeeMapper.deleteById(eid);
    }
}
